﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CocisDAL;
using CocisModel;
using System.Data;
using CocisWeb.src;

namespace CocisWeb.Report
{
    public partial class CommonSearch : System.Web.UI.Page
    {
        public TypesDAL dal = new TypesDAL();
        public AssetDAL assetDAL = new AssetDAL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.BindSiteCode();

                this.ddlEnvironment.Enabled = true;
                this.ddlPhysical_VM.Enabled = true;
                this.ddlStatus.Enabled = true;
                this.ddlHostingType.Enabled = true;
                this.ddlBU.Enabled = true;
              

                this.ddlDeviceType.Enabled = false;
                this.ddlClusterType.Enabled = false;
                this.ddlNLBMode.Enabled = false;
                this.ddlDBType.Enabled = false;
                this.ddlRescoveryModel.Enabled = false;
                this.ddlCVCoverage.Enabled = false;

            }
        }

        #region 绑定基本信息
        /// <summary>
        /// 绑定基本信息
        /// </summary>
        public void BindSiteCode()
        {
            ListItem item = new ListItem("All","0");

            this.cbxlSiteCode.DataSource = assetDAL.GetSiteCode();
            this.cbxlSiteCode.DataTextField = "siteCode";
            this.cbxlSiteCode.DataValueField = "siteCodeID";
            this.cbxlSiteCode.DataBind();

            for (int i = 0; i < this.cbxlSiteCode.Items.Count; i++)
            {
                this.cbxlSiteCode.Items[i].Selected = true;
            }

            //this.ddlDBType.DataSource = assetDAL.GetDBTypeInfo();
            //this.ddlDBType.DataTextField = "DBType";
            //this.ddlDBType.DataValueField = "DBTypeID";
            //this.ddlDBType.DataBind();
            //this.ddlDBType.Items.Add(item);
            //this.ddlDBType.SelectedValue = "0";

            this.ddlEnvironment.DataSource = assetDAL.GetEnvironment();
            this.ddlEnvironment.DataTextField = "Environment";
            this.ddlEnvironment.DataValueField = "EnvironmentID";
            this.ddlEnvironment.DataBind();
            this.ddlEnvironment.Items.Add(item);
            this.ddlEnvironment.SelectedValue = "0";

            this.ddlHostingType.DataSource = assetDAL.GetHostingType();
            this.ddlHostingType.DataTextField = "HostingType";
            this.ddlHostingType.DataValueField = "HostingTypeID";
            this.ddlHostingType.DataBind();
            this.ddlHostingType.Items.Add(item);
            this.ddlHostingType.SelectedValue = "0";

            this.ddlBU.DataSource = assetDAL.GetBU();
            this.ddlBU.DataTextField = "BU";
            this.ddlBU.DataValueField = "BUID";
            this.ddlBU.DataBind();
            this.ddlBU.Items.Add(item);
            this.ddlBU.SelectedValue = "0";

            this.ddlCVCoverage.DataSource = assetDAL.GetCVCoverage();
            this.ddlCVCoverage.DataTextField = "CVCoverage";
            this.ddlCVCoverage.DataValueField = "CVCoverageID";
            this.ddlCVCoverage.DataBind();
            this.ddlCVCoverage.Items.Add(item);
            this.ddlCVCoverage.SelectedValue = "0";


            this.ddlNLBMode.DataSource = assetDAL.GetNLBMode();
            this.ddlNLBMode.DataTextField = "NLBMode";
            this.ddlNLBMode.DataValueField = "NLBModeID";
            this.ddlNLBMode.DataBind();
            this.ddlNLBMode.Items.Add(item);
            this.ddlNLBMode.SelectedValue = "0";

            this.ddlStatus.DataSource = assetDAL.GetStatus();
            this.ddlStatus.DataTextField = "Status";
            this.ddlStatus.DataValueField = "StatusID";
            this.ddlStatus.DataBind();

            this.ddlStatus.Items.Add(item);

        }
        #endregion

        protected void ddlAssetType_SelectedIndexChanged(object sender, EventArgs e)
        {
            int type =int.Parse(this.ddlAssetType.SelectedValue.ToString());
            if (type == 1)//系统节点
            {
                this.ddlEnvironment.Enabled = true;
                this.ddlPhysical_VM.Enabled = true;
                this.ddlStatus.Enabled = true;
                this.ddlHostingType.Enabled = true;
                this.ddlBU.Enabled = true;
            


                this.ddlDeviceType.Enabled = false;
                this.ddlClusterType.Enabled = false;
                this.ddlNLBMode.Enabled = false;
                this.ddlDBType.Enabled = false;
                this.ddlRescoveryModel.Enabled = false;
                this.ddlCVCoverage.Enabled = false;
 
            }
            if (type == 2)//物理服务器
            {
                this.ddlEnvironment.Enabled = false;
                this.ddlPhysical_VM.Enabled = false;
                this.ddlStatus.Enabled = true;
                this.ddlHostingType.Enabled = false;
                this.ddlBU.Enabled = false;
              

                this.ddlDeviceType.Enabled = true;
                this.ddlClusterType.Enabled = false;
                this.ddlNLBMode.Enabled = false;
                this.ddlDBType.Enabled = false;
                this.ddlRescoveryModel.Enabled = false;
                this.ddlCVCoverage.Enabled = false;




            }
            if (type == 3)//虚拟服务器
            {
                this.ddlEnvironment.Enabled = false;
                this.ddlPhysical_VM.Enabled = false;
                this.ddlStatus.Enabled = true;
                this.ddlHostingType.Enabled = false;
                this.ddlBU.Enabled = false;
             

                this.ddlDeviceType.Enabled = false;
                this.ddlClusterType.Enabled = false;
                this.ddlNLBMode.Enabled = false;
                this.ddlDBType.Enabled = false;
                this.ddlRescoveryModel.Enabled = false;
                this.ddlCVCoverage.Enabled = false;
            }
            if (type == 4)//集群服务器
            {
                this.ddlEnvironment.Enabled = false;
                this.ddlPhysical_VM.Enabled = false;
                this.ddlStatus.Enabled = false;
                this.ddlHostingType.Enabled = false;
                this.ddlBU.Enabled = false;
             

                this.ddlDeviceType.Enabled = false;
                this.ddlClusterType.Enabled = true;
                this.ddlNLBMode.Enabled = true;
                this.ddlDBType.Enabled = false;
                this.ddlRescoveryModel.Enabled = false;
                this.ddlCVCoverage.Enabled = false;

            }
            if (type == 5)//数据库实例
            {
                this.ddlEnvironment.Enabled = false;
                this.ddlPhysical_VM.Enabled = false;
                this.ddlStatus.Enabled = true;//
                this.ddlHostingType.Enabled = false;
                this.ddlBU.Enabled = false;
           
                this.ddlDeviceType.Enabled = false;
                this.ddlClusterType.Enabled = false;
                this.ddlNLBMode.Enabled = false;
                this.ddlDBType.Enabled = true;//
                this.ddlRescoveryModel.Enabled = false;
                this.ddlCVCoverage.Enabled = false;

            }
            if (type == 6)//数据库具体
            {
                this.ddlEnvironment.Enabled = false;
                this.ddlPhysical_VM.Enabled = false;
                this.ddlStatus.Enabled = true;//
                this.ddlHostingType.Enabled = false;
                this.ddlBU.Enabled = false;
              

                this.ddlDeviceType.Enabled = false;
                this.ddlClusterType.Enabled = false;
                this.ddlNLBMode.Enabled = false;
                this.ddlDBType.Enabled = false;
                this.ddlRescoveryModel.Enabled = true;//
                this.ddlCVCoverage.Enabled = true;//
            }
            this.BindDataColumns(type);

        }

        public void BindDataColumns(int type)
        {
            DataTable dt = new DataTable();
            if (type == 1)//系统节点
            {
                dt = assetDAL.GetServerColumn(1).Tables[0];//获取字段名称

            }
            if (type == 2)//物理服务器
            {
                

            }
            if (type == 3)//虚拟服务器
            {
               
            }
            if (type == 4)//集群服务器
            {
               

            }
            if (type == 5)//数据库实例
            {
                

            }
            if (type == 6)//数据库具体
            {
                
            }
            DataTable dtl = new DataTable();
            dtl.Columns.Add("columnID");
            dtl.Columns.Add("columnName");
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                DataRow dr = dtl.NewRow();
                if (!dt.Columns[j].ColumnName.ToString().ToLower().EndsWith("id"))
                {
                    dr[0] = dt.Columns[j].ColumnName.ToString();
                    dr[1] = dt.Columns[j].ColumnName.ToString();
                    dtl.Rows.Add(dr);
                }

            }
            this.cblColumn.DataSource = dtl;
            this.cblColumn.DataTextField = "ColumnName";
            this.cblColumn.DataValueField = "ColumnID";
            this.cblColumn.DataBind();

           
            


           
 
        }

        protected void cbxAllSiteCode_CheckedChanged(object sender, EventArgs e)
        {
            if (this.cbxAllSiteCode.Checked == true)
            {
                for (int i = 0; i < this.cbxlSiteCode.Items.Count; i++)
                {
                    this.cbxlSiteCode.Items[i].Selected = true;
                }
            }
            else
            {
                for (int i = 0; i < this.cbxlSiteCode.Items.Count; i++)
                {
                    this.cbxlSiteCode.Items[i].Selected = false;
                }
 
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            int type = int.Parse(this.ddlAssetType.SelectedValue.ToString());
            string serverName = this.txtServerName.Text;
            string serverIP = this.txtServerIP.Text;
            string sn = this.txtSN.Text;
            string clusterName = this.txtClusterName.Text;
            string assetNo = this.txtAssetNo.Text;
            string uniqueNo = this.txtUniqueNo.Text;
            string fullInstanceName = this.txtFullInstanceName.Text;

            int environmentId = int.Parse(this.ddlEnvironment.SelectedValue.ToString());
            string physical_VM = this.ddlPhysical_VM.SelectedValue.ToString();
            int statusID = int.Parse(this.ddlStatus.SelectedValue.ToString());
            int hostingTypeID = int.Parse(this.ddlHostingType.SelectedValue.ToString());
            int BUID = int.Parse(this.ddlBU.SelectedValue.ToString());
            int deviceTypeID = int.Parse(this.ddlDeviceType.SelectedValue.ToString());
            int nlbModeId = int.Parse(this.ddlNLBMode.SelectedValue.ToString());
            string clusterType = this.ddlClusterType.SelectedValue.ToString();
            int dbTypeID = int.Parse(this.ddlDBType.SelectedValue.ToString());
            string recoveryModel=this.ddlRescoveryModel.SelectedValue.ToString();
            int cvBackupID=int.Parse(this.ddlCVCoverage.SelectedValue.ToString());
            string siteCodeInfo = "";//先赋空值
            for (int i = 0; i < this.cbxlSiteCode.Items.Count; i++)//循环站点信息
            {
                if (this.cbxlSiteCode.Items[i].Selected == true)
                {
                    siteCodeInfo += this.cbxlSiteCode.Items[i].Value.ToString()+",";
                }
 
            }
            if (siteCodeInfo != "")//如果有选择站点信息，则把最后个“，”去掉
            {
                siteCodeInfo = siteCodeInfo.ToString().TrimEnd(',');
            }

            if (type == 1)//系统节点
            {
                DataTable dt = new DataTable();
                dt = assetDAL.GetServer(serverName, serverIP, environmentId, physical_VM, statusID, hostingTypeID, BUID, siteCodeInfo).Tables[0];

                //控制显示列开始
                for (int k = 0; k < this.cblColumn.Items.Count; k++) //遍历属性
                {

                    if (this.cblColumn.Items[k].Selected == false)//如果不勾选
                    {

                            dt.Columns.Remove(this.cblColumn.Items[k].Text);//移除列 


                    }
                }
                List<string> dtc = new List<string>();
      

                foreach (DataColumn item in dt.Columns)
                {
                    dtc.Add(item.ColumnName);
                }
                foreach (string item in dtc)
                {
                    if (item.ToLower().EndsWith("id"))
                    {
                        dt.Columns.Remove(item);//移除列 
                    }  
                }






                this.gvAssets.DataSource = dt.DefaultView;
                this.gvAssets.DataBind();



                //显示列结束

            }

            //else if (type == 2)//物理服务器
            //{
            //    this.gvAssets.DataSource = assetDAL.SearchPhysicalServer(assetNo,sn,statusID,siteCodeInfo,deviceTypeID);
            //    this.gvAssets.DataBind();

            //}
            //else if (type == 3)//虚拟服务器
            //{
            //    this.gvAssets.DataSource = assetDAL.SearchVirtualServer(assetNo,statusID,siteCodeInfo);
            //    this.gvAssets.DataBind();
            //}
            //else if (type == 4)//集群服务器
            //{
            //    this.gvAssets.DataSource = assetDAL.SearchClusterServer(clusterName,clusterType,fullInstanceName,nlbModeId,siteCodeInfo);
            //    this.gvAssets.DataBind();

            //}
            //else if (type == 5)//数据库实例
            //{
            //    this.gvAssets.DataSource = assetDAL.SearchDBInstance(fullInstanceName,serverName,dbTypeID,statusID,siteCodeInfo);
            //    this.gvAssets.DataBind();

            //}
            //else if (type == 6)//数据库具体
            //{
            //    this.gvAssets.DataSource = assetDAL.SearchDataBase(fullInstanceName,recoveryModel,cvBackupID,statusID,siteCodeInfo);
            //    this.gvAssets.DataBind();
            //}
                



        }

        protected void btnExcel_Click(object sender, EventArgs e)
        {

        }

        protected void cblColumn_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void cblColumn_TextChanged(object sender, EventArgs e)
        {
            foreach (ListItem item in cblColumn.Items)
            {
                
                if (item.Value.ToString() == "Environment"  )
                {
                    Label1.Visible = false;
                    ddlEnvironment.Visible = false;
                    if (item.Selected)
                    {
                        Label1.Visible = true;
                        ddlEnvironment.Visible = true;
                    }
                }


            }
        }

        protected void ChkBoxAllAssetType_CheckedChanged (object sender, EventArgs e)
        {
            if (this.ChkBoxAllAssetType.Checked == true)
            {
                for (int i = 0; i < this.cblColumn.Items.Count; i++)
                {
                    this.cblColumn.Items[i].Selected = true;
                }
            }
            else
            {
                for (int i = 0; i < this.cblColumn.Items.Count; i++)
                {
                    this.cblColumn.Items[i].Selected = false;
                }

            }
        }

        
        
    }
}
